package org.hengguan.api.contest.vo;

import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@Data
@Accessors(chain = true)
@ToString
public class ExceptionVo<T extends Exception> {
	
	public ExceptionVo() {
		
	}
	public ExceptionVo(T t) {
		this.exName = t.getClass().getName();
		this.exMsg = t.getMessage();
		if (t.getCause() != null) {
			this.exName = t.getCause().getClass().getName();
		}
		if (t instanceof BindException) {
			this.exMsg = ((BindException) t).getFieldError().getDefaultMessage();
		}
		if (t instanceof MethodArgumentNotValidException) {
			this.exMsg = ((MethodArgumentNotValidException) t).getBindingResult().getFieldError().getDefaultMessage();
		}
//		if (t instanceof ConstraintViolationException) {
//			this.exMsg = StrUtil.split(t.getMessage(),".")[1];
//		}
		if (t instanceof MissingServletRequestParameterException) {
			this.exMsg = ((MissingServletRequestParameterException) t).getParameterName()+" => not found";
		}
		if (t instanceof MethodArgumentTypeMismatchException) {
			this.exMsg = ((MethodArgumentTypeMismatchException) t).getName()+" => invalid type";
		}

	}
	
	private String exName;
	
	private String exMsg;
	
}
